package com.gdyd.boot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2         //开启Swagger2
public class SwaggerConfig {

    //Swagger的Bean实例Docket（http://localhost:8082/swagger-ui.html）

    @Bean
    public Docket docket(Environment environment){
        //配置要显示Swagger的环境
        Profiles profiles = Profiles.of("dev","test");
        //获取项目的环境
        final boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .groupName("Trace")
                    .enable(flag)       //启用
                    .select()
                    //RequestHandlerSelectors:配置要扫描注解的方式
                    //any():扫描全部
                    //none():不扫描
                    //withClassAnnotation(RestController.class)扫描类上的注解
                    //withMethodAnnotation(GetMapping.class)扫描方法上的注解
                    .apis(RequestHandlerSelectors.basePackage("com.gdyd.boot.controller"))  //只要接口中返回值存在实体类，就会被扫描到swagger中
                    //.paths("...")过滤路径
                    .build();
    }

//    @Bean
//    public Docket docket_erp(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("ERP");
//    }


    private ApiInfo apiInfo(){
        Contact contact = new Contact("沈泽群", "https://trace001.gitee.io/", "2586883143@qq.com");
        return new ApiInfo( "SpringBoot HelloWorld",
                            "读书多了，容颜自然改变",
                            "1.0",
                            "https://trace001.gitee.io",
                             contact,
                            "Apache 2.0",
                            "http://www.apache.org/licenses/LICENSE-2.0",
                             new ArrayList());
    }
}
